//
//  TUserInfoViewModel.m
//  LoveShare
//
//  Created by ways on 2017/7/6.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TUserInfoViewModel.h"

@implementation TUserInfoViewModel


- (instancetype)initUserInfoViewModel {
    self = [super init];
    
    if (self) {
        
        [self initialBind];
    }
    
    return self;
}


#pragma mark - init


- (TUserInfoModel *)userInfoModel{
    if (!_userInfoModel) {
        _userInfoModel = [[TUserInfoModel alloc] init];
    }
    return _userInfoModel;
}

- (void)initialBind{
    
    // 处理登录业务逻辑
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setValue:[TUserManager defaultManager].userInfoModel.result.userId forKey:@"userId"];
            [params setValue:self.userInfoModel.nickname forKey:@"nickname"];
            [params setValue:self.userInfoModel.sex forKey:@"sex"];
            [params setValue:self.userInfoModel.communityId forKey:@"communityId"];
            [params setValue:self.userInfoModel.roomNum forKey:@"roomNum"];
            
            NSMutableDictionary *authority = [NSMutableDictionary dictionary];
            [authority setValue:@"abcdefg" forKey:@"token"];
            NSMutableDictionary *paramsBig = [NSMutableDictionary dictionary];
            [paramsBig setValue:params forKey:@"params"];
            [paramsBig setValue:authority forKey:@"authority"];
            NSData *body = [NSJSONSerialization dataWithJSONObject:paramsBig options:NSJSONWritingPrettyPrinted error:nil];
            
            /*
             {"clientInfo":{"appVersion":"1.0","os":"ios"},"authority":{"token":"abcdefg"},
             "params":{"userId":1,"nickname":"uesa","sex":0,"communityId":1,"roomNum":"A1001"}}
             */
            [TServiceTool POST:T_UpdateInfo body:body parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary  *_Nullable responseObject) {
                //                NSLog(@"responseObject:%@",responseObject);
                
                if ([responseObject isKindOfClass:[NSDictionary class]]) {
                    [self.userInfoModel mj_setKeyValues:responseObject];
                    if ([self.userInfoModel.msgCode intValue] == 100) {//提交成功
                        
                    } else {
                        [SVProgressHUD dismissWithDelay:0.5];
                        [SVProgressHUD showErrorWithStatus:self.userInfoModel.message];
                    }
                }
                
                [subscriber sendNext:self.userInfoModel];
                // 数据传送完毕，必须调用完成，否则命令永远处于执行状态
                [subscriber sendCompleted];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"error:%@",error.description);
                [subscriber sendError:error];
                [subscriber sendCompleted];
                [SVProgressHUD showErrorWithStatus:@"服务器异常"];
            }];
            
            return nil;
            
        }];
        
        
        return signal;
    }];
    
    //监听登录产生的数据
    [_requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        
    }];
    
    // 监听登录状态
    @weakify(self)
    [[_requestCommand.executing skip:1] subscribeNext:^(id x) {
        @strongify(self)
        if ([x isEqualToNumber:@(YES)]) {
            
            // 用蒙版提示
            [SVProgressHUD showWithStatus:@"正在提交..."];
            
        }else {
            
            //隐藏蒙版
            if ([self.userInfoModel.msgCode intValue] == 100) {//提交成功
                [SVProgressHUD dismiss];
            }
            
        }
    }];
}

@end










